#!/bin/csh

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# FERRET MAPS OF TROPICAL STORM TRAJECTORIES
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#=====================================================================
# --- INPUT
#=====================================================================

if ( $#argv == 0 ) then
#-------------------------------------------------
echo    "     "
echo -n "    INPUT DIRECTORY  = " ; set adir     = `echo $<`
echo -n "    OUTPUT DIRECTORY = " ; set odir     = `echo $<`
echo -n "   FIRST YEAR [1979] = " ; set yrbeg    = `echo $< `
echo -n "    LAST YEAR [1979] = " ; set yrend    = `echo $< `
echo -n "         EXPERIMENTS = " ; set exp_list = `echo $<`
echo    "     "
if ( $#yrbeg  == 0 ) set yrbeg  = 1979
if ( $#yrend  == 0 ) set yrend  = 1979
echo    "     "
#-------------------------------------------------
else
#-------------------------------------------------
set adir     = $1 ; shift
set odir     = $1 ; shift
set yrbeg    = $1 ; shift
set yrend    = $1 ; shift
set exp_list = `echo $argv`
#-------------------------------------------------
endif

#=====================================================================
# --- PATHNAMES & DIRECTORIES
#=====================================================================

# --- input  directory
if ( $#adir == 0 ) set adir = /home/jjs/tstorms_wehner/sample_output

# --- output directory
if ( $#odir   == 0 ) set odir = /home/jjs/tstorms_wehner/sample_output
if ( ! -e $odir ) mkdir -p $odir
cd $odir
  
#--- ferret stuff
set fdir = /home/jjs/tstorms_wehner/scripts/my_ferret_go

#=====================================================================
# --- GET DATA FOR PLOTTING
#=====================================================================

set xargs = dummy
set ntraj = dummy

                            set tyear = ${yrbeg}
if ( ${yrbeg} != ${yrend} ) set tyear = ${yrbeg}-${yrend}

foreach exp ( $exp_list )
#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
set expx = `echo $exp | tr "_" "-"`


# --- extract ori data
       @ year  = $yrbeg
while ( $year <= $yrend)
     set fin  =  ${adir}/${exp}/ori_${year}
if ( -e $fin  ) then
cat $fin >> fin.${expx}.${tyear}
endif
@ year ++
end

set nts = `wc -l fin.${expx}.${tyear}`

set xargs = ($xargs ${expx}.${tyear})

# --- extract traj data

       @ indx  = 0
       @ year  = $yrbeg
while ( $year <= $yrend)
@ indx ++
set ftraj = ${adir}/${exp}/traj_${year}
set nf0   = `wc -l $ftraj`
@ nf = $nf0[1] + 1
set spos  = ` grep -n start $ftraj | cut -f1 -d":" `
set nspos = $#spos
set spos = ($spos $nf )
@ i = 1
while ( $i <= $nspos )
@ ip = $i + 1
@ ih = $spos[$ip] - 1
@ it = $ih - $spos[$i] 
head -${ih} $ftraj | tail -${it} | awk '{ printf "%8.2f%8.2f \n", $1,$2}' >> ftraj.${exp}.${indx}
@ i ++
@ indx ++
end
@ year ++
end

@ indx --
set ntraj = ( $ntraj $indx )

echo  ${expx}.${tyear} $nts[1]
#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
end
echo "    "

shift xargs
shift ntraj

#=====================================================================
# --- GET & SET JNL FILES
#=====================================================================

set nexp = $#exp_list

cp ${fdir}/domain_40ns.jnl         domain.jnl
cp ${fdir}/nlots_${nexp}_40ns.jnl  nlots.jnl
cp ${fdir}/ts_ori.jnl              ts_ori.jnl
cp ${fdir}/ts_traj.jnl             ts_traj.jnl

cat << EOF > fscript.jnl
\cancel mode verify
set mode metafile
go nlots.jnl
EOF

@ i = 0
foreach exp ( $exp_list )
@ i ++
if ( $nexp > 1 ) then
echo "set viewport v${i}" >> fscript.jnl
endif
echo "go ts_ori.jnl $xargs[$i]" >> fscript.jnl
@ indx = 1
while ( $indx <= $ntraj[$i] )
echo "go ts_traj.jnl ftraj.${exp}.${indx}" >> fscript.jnl
@ indx ++
end
end

echo "quit" >> fscript.jnl

#=====================================================================
# --- PLOT MAP
#=====================================================================

set pict = map_traj

ferret << EOF
go fscript.jnl
quit
EOF

/usr/local/ferret/bin/gksm2ps -l cps -o ${pict}.ps metafile.plt

mgv -noantialias ${pict}.ps
            gzip ${pict}.ps

#=====================================================================
# --- CLEAN UP
#=====================================================================

foreach exp ( $exp_list )
  set expx = `echo $exp | tr "_" "-"`
  rm -f fin.${expx}.*
  rm -f ftraj.${exp}.*
end

rm -f  ferret.jnl*
rm  -f *.plt
rm  -f *.jnl

echo "   "
echo " *** fmaps done *** "
echo "   "

#=====================================================================
exit
